package com.jiaai.cloud.base.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/29.
 */
public class JWTEncryptUtil {
    private final  static  String  SECRET="WEIXINAPP";

    public    static  String  encrypt(Map<String,String> datas)throws Exception{
        Map<String,Object> headers=new HashMap();
        headers.put("typ","JWT");
        headers.put("alg","HS256");
        JWTCreator.Builder  builder=JWT.create();
        builder.withHeader(headers);
        for (Map.Entry<String, String> entry : datas.entrySet()) {
            builder.withClaim(entry.getKey(),entry.getValue());
        }
        return builder.sign(Algorithm.HMAC256(SECRET));

    }

    public  static  Map<String,Claim> decrypt(String encrypts)throws Exception{
        JWTVerifier verifier = null;
         verifier = JWT.require(Algorithm.HMAC256(SECRET))
                    .build();
            DecodedJWT jwt = verifier.verify(encrypts);
            Map<String, Claim> claims = jwt.getClaims();

            return claims;


    }
}
